import {Component, OnInit} from '@angular/core';
import {ApiService} from '@core/services/http/api.service';
import {dateFormat, replaceAll, type} from '../../../utils/utils';
import {ApesMessageService} from '@shared/apes/message';
import {GridConfigService} from '@core/config/grid-config.service';
import {HttpClient} from '@angular/common/http';
import {FormlyFieldConfig, FormlyFormOptions} from '@ngx-formly/core';
import {FormGroup} from '@angular/forms';

@Component({
  selector: 'service-order-flow',
  templateUrl: './service-order-flow.component.html',
  styleUrls: ['./service-order-flow.component.scss'],
})
export class ServiceOrderFlowComponent implements OnInit {

  height: any = 500 + 'px';

  /** 按钮相关 **/
  buttonBar = [
    { hide: true, style: 'primary', label: '查询', id: 'action_query', loading: false },
  ];


  form = new FormGroup({});
  ctx = { model: {} };
  model: any = {};
  options: FormlyFormOptions = {};
  field: FormlyFieldConfig[] = [
    {
      key: 'fsmFlow#id',
      type: 'conditionInput',
      templateOptions: {
        label: '状态流',
        placeholder: '请输入状态流编码或名称',
        config: {
          api: 'fsm.fsmFlow.findAll',
          domain: '{"filters":[{"field":"id","operator":"like","value":"{{value}}"},{"field":"name","operator":"like","value":"{{value}}"}],"groups":[],"operator":"and"}',
          init: true,
          operators: ['EQ'],
          type: 'select',
          downFormatter: '\'[ \' + value.id + \' ]\' + \' \' + value.name',
          valueFormatter: '\'[ \' + value.id + \' ]\' + \' \' + value.name',
        },
      },
    },
    {
      key: 'businessKey',
      type: 'input',
      templateOptions: {
        label: '业务单号',
      },
    },

    {
      key: 'createDate',
      type: 'conditionInput',
      templateOptions: {
        label: '创建时间',
        config: {
          inable: false,
          nullable: false,
          operators: ['EQ'],
          type: 'dateRange',
        }
      },
    },
  ];



  constructor(private apiService: ApiService,
              private msg: ApesMessageService,
              private http: HttpClient,
              private gridConfigService: GridConfigService) {


  }

  ngOnInit() {

  }

  /**
   * 功能：按钮事件路由
   * @param data 当前按钮元数据
   */
  onEvent(data: any) {
    if (data instanceof MouseEvent) return;
    if (data) {
      if (data.id === 'action_query') {
        this.buttonBar[0].loading = true;
        this.action_query();
      }
    }
  }

  /** 主数据 查询按钮事件 **/
  action_query() {

    this.queryData();
  }

  queryData() {
    console.log(this.model);
    const conditionParam = this.getConditionList(this.field);

    const sendData = {};
    sendData['condition'] = conditionParam.conditionList;

  }

  /**
   * 功能：拼接查询条件
   * @returns {any[]}
   */
  getConditionList(conditionField: any): { flag: boolean, conditionList: any[] } {
    // flag 用于判断必输项是否输入完成
    let returnParam = { flag: true, conditionList: [] };
    let conditionList = [];
    for (const tempConditionField of conditionField) {
      let fieldKey = tempConditionField.key;

      let fieldValue = this.model[fieldKey];

      const  config = tempConditionField.templateOptions.config;

      // 判断查询的操作条件（=、like...）
      let operatorValue = '';

      if (fieldValue === undefined || fieldValue === null || fieldValue.length === 0) {
        continue;
      }
      // 处理存在明细表字段的情况
      fieldKey = replaceAll(fieldKey, '#', '.');

      if (config && config.type === 'dateRange') {
        const dataModelStar = {
          field: fieldKey,
          operator: 'GTE',
          value: dateFormat('yyyy-MM-dd', fieldValue[0]) + ' 00:00:00',
          type: 'date',
        };
        const dataModelEnd = {
          field: fieldKey,
          operator: 'LTE',
          value: dateFormat('yyyy-MM-dd', fieldValue[1]) + ' 23:59:59',
          type: 'date',
        };

        conditionList.push(dataModelStar);
        conditionList.push(dataModelEnd);
      } else {
        const dataModel = {};

        dataModel['field'] = fieldKey;
        dataModel['operator'] = operatorValue ? operatorValue : 'EQ';
        dataModel['value'] = type(fieldValue) == 'string' ?
          fieldValue.trim() : fieldValue;
        conditionList.push(dataModel);
      }
    }
    returnParam.conditionList = conditionList;
    return returnParam;
  }

}

